package com.c.ctgproject.ui.collect

import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPartHandlerBinding
import com.c.ctgproject.entity.guang.PartHandlerListEntity
import com.c.ctgproject.entity.guang.PartOutListEntity
import com.c.ctgproject.event.PositionsEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.EventBus
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

/**
 *作者:LiBW
 *创建日期: 2021/9/17
 *描述: 配件拆解 页面 (零部件)
 */
class PartHandlerActivity : BaseActivityV2() {
   private val mBinding by lazy { ActivityPartHandlerBinding.inflate(layoutInflater) }
   override fun getLayout() = mBinding.root
    private val  selectList by lazy { intent.getSerializableExtra(MyConstants.DATA) as ArrayList<PartOutListEntity.DataBean.ListBean> }
    private var time = ""
    private val partsIdList =ArrayList<Int>()

    override fun event() {
        selectList.forEach {
            partsIdList.add(it.partsId)
        }

        val startDate = Calendar.getInstance()
        val endDate = Calendar.getInstance()
        endDate[2030, 11] = 31
        val pvTime = TimePickerBuilder(this) { date, v ->
            val format = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
            time = format.format(date)
            mBinding.mTimeTV.text = time
            MyLog.d("选择的时间:$date  ${getGson().toJson(date)}")
        }.setRangDate(startDate, endDate)
            .build()
        mBinding.mTimeLL.setOnClickListener {
            pvTime.show()
        }
   }

   override fun click() {
       mBinding.mConfirmBTN.setOnClickListener {
           if (time.isEmpty()) {
               showToast("请选择计划完成时间")
               mBinding.mTimeLL.myShake()
           } else {
               val map = HashMap<String, Any>()
               map["partsIdList"]=partsIdList
               map["plainFinishTime"]=time
               MyLog.d(handlerBody(map))
               ApiMethods.apiSubscribe(ApiMethods.retrofit.createPartHandlerOrder(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                   MyLog.d("拆解回调:$it")
                   showToast("操作成功")
                   val event=PositionsEvent(partsIdList)
                   EventBus.getDefault().post(event)
                   finish()
               })
           }
       }
   }
}